{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "一、两向量的数量积\n",
    "设一物体在恒力F作用下沿直线从点M移动到点M,以s表示位移M,M,.由物理学知道,力F所作的功为\n",
    "W=lFllslcos 0,\n",
    "其中0为F与s的夹角(图8-18).从这个问题看出,我们有时要对两个向量a和b作这样的运算,运算的结果是一个数,它等于la1、lb1及它们的夹角的余弦的乘积.我们把它叫做向量a与b的数量积,记作a·b(图8-19),即\n",
    "a·b=lal lblcos 0."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "例 已知三点M(1，1，1)，A(2，2，1)和B(2，1，2),求∠AMB."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "60.00000000000001\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "\n",
    "# 定义点坐标\n",
    "M = np.array([1, 1, 1])\n",
    "A = np.array([2, 2, 1])\n",
    "B = np.array([2, 1, 2])\n",
    "\n",
    "# 计算向量 MA 和向量 MB\n",
    "MA = A - M\n",
    "MB = B - M\n",
    "\n",
    "# 计算向量点积\n",
    "dot_product = np.dot(MA, MB)\n",
    "\n",
    "# 计算向量MA和MB的模长\n",
    "norm_MA = np.linalg.norm(MA)\n",
    "norm_MB = np.linalg.norm(MB)\n",
    "\n",
    "# 通过向量点积公式计算夹角的余弦值\n",
    "cos_angle = dot_product / (norm_MA * norm_MB)\n",
    "\n",
    "# 根据余弦值求夹角（弧度制转角度制）\n",
    "angle = np.arccos(cos_angle) * 180 / np.pi\n",
    "\n",
    "print(angle)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "二、两向量的向量积"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "例 设向量a=(2,1,-1),向量b=(1,-1,2).计算向量a×向量b."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1 -5 -3]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# 定义向量a\n",
    "a = np.array([2, 1, -1])\n",
    "# 定义向量b\n",
    "b = np.array([1, -1, 2])\n",
    "\n",
    "# 计算向量a与向量b的叉积\n",
    "cross_product = np.cross(a, b)\n",
    "\n",
    "print(cross_product)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "例 已知三角形ABC的顶点分别是A(1，2,3)、B(3,4，5)和C(2，4,7).求三角形ABC的面积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.7416573867739413\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "\n",
    "# 定义点坐标\n",
    "A = np.array([1, 2, 3])\n",
    "B = np.array([3, 4, 5])\n",
    "C = np.array([2, 4, 7])\n",
    "\n",
    "# 计算向量AB\n",
    "AB = B - A\n",
    "# 计算向量AC\n",
    "AC = C - A\n",
    "\n",
    "# 计算向量AB和AC的叉积\n",
    "cross_product = np.cross(AB, AC)\n",
    "\n",
    "# 计算叉积向量的模长\n",
    "norm_cross_product = np.linalg.norm(cross_product)\n",
    "\n",
    "# 根据三角形面积公式（向量叉积模长的一半）计算面积\n",
    "area = norm_cross_product / 2\n",
    "\n",
    "print(area)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "三、向量的混合积\n",
    "设已知三个向量ab和c.先作两向量a和b的向量积axb.把所得到的向\n",
    "量与第三个向量 再作数量积(axb)·c.这样得到的数量叫做三向量a、b.c的\n",
    "混合积,记作[abc]."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "例 已知A(1，2，0)，B(2，3，1)，C(4，2，2)，M(x，y，z)四点共面，求点M的坐标x，y，z所满足的关系式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Eq(2*x + y - 3*z - 4, 0)\n"
     ]
    }
   ],
   "source": [
    "import sympy\n",
    "\n",
    "# 定义变量\n",
    "x, y, z = sympy.symbols('x y z')\n",
    "\n",
    "# 定义点坐标\n",
    "A = sympy.Matrix([1, 2, 0])\n",
    "B = sympy.Matrix([2, 3, 1])\n",
    "C = sympy.Matrix([4, 2, 2])\n",
    "M = sympy.Matrix([x, y, z])\n",
    "\n",
    "# 计算向量 AB、AC、AM\n",
    "AB = B - A\n",
    "AC = C - A\n",
    "AM = M - A\n",
    "\n",
    "# 计算混合积并令其等于0\n",
    "mixed_product = AB.cross(AC).dot(AM)\n",
    "equation = sympy.Eq(mixed_product, 0)\n",
    "print(equation)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
